package com.srd.phone.activity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.shengrongdai.srd.R;
import com.srd.phone.application.AppActivityManager;
import com.srd.phone.application.BaseApplication;
import com.srd.phone.mvp.interfaces.IBaseView;
import com.srd.phone.mvp.presenters.BasePresenter;
import com.srd.phone.utils.Constants;
import com.srd.phone.utils.LogUtils;
import com.srd.phone.utils.PreferenceUtils;
import com.srd.phone.utils.ProgressHUD;

public class BaseActivity extends AppCompatActivity implements IBaseView {

    public RequestQueue requestQueue;
    public Context mContext;
    public FrameLayout fl_common_head;
    public LinearLayout ll_common_back;
    public TextView tv_common_title;
    public TextView tv_common_right;

    private FrameLayout fl_parent_content;

    //关于网络请求
    public BasePresenter presenter;
    private String[] urlArrs = null;

    public BaseApplication application;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initBaseData();
        getResolution();
    }

    private void initBaseData() {
        AppActivityManager.getInstance().addActivity(this); //添加activity到栈中
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // 设置竖屏
        application = (BaseApplication) getApplication();
        requestQueue = application.getRequestQueue(); //获取volley请求队列
        mContext = this;
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(R.layout.activity_base_layout);
        fl_common_head = (FrameLayout) findViewById(R.id.fl_common_head);
        fl_parent_content = (FrameLayout) findViewById(R.id.fl_parent_content);
        fl_parent_content.addView(getLayoutInflater().inflate(layoutResID, null));
        ll_common_back = (LinearLayout) findViewById(R.id.ll_common_back);
        ll_common_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_common_title = (TextView) findViewById(R.id.tv_common_title);
        tv_common_right = (TextView) findViewById(R.id.tv_common_right);
    }

    private void getResolution() {

        if (PreferenceUtils.getInstance().getData(Constants.APP_WIDTH, 0) == 0) {
            PreferenceUtils.getInstance().putData(Constants.APP_WIDTH, getWindowManager().getDefaultDisplay().getWidth());
            PreferenceUtils.getInstance().putData(Constants.APP_HEIGHT, getWindowManager().getDefaultDisplay().getHeight());
        }
    }

    /**
     * 获取登录状态  true： 登录    false ： 未登录
     */
    public boolean getLoginStatus() {
        return PreferenceUtils.getInstance().getData(Constants.LOGIN_STATUS, false);
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void returnData(String tag, String data) {

    }

    @Override
    public void onError(String tag, VolleyError error) {
        Toast.makeText(BaseActivity.this, "数据请求失败", Toast.LENGTH_SHORT).show();
        cancelWaitingDialog();
        LogUtils.i(BaseActivity.class, error.statusCode + "   " + error.getMessage());
    }

    /**
     * 跳转界面
     * @param cls
     */
    public void gotoActivity(Class<?> cls) {
        startActivity(new Intent(this, cls));
    }

    public void gotoActivity(Intent intent){
        startActivity(intent);
    }

    //添加fragment
    public void replaceFragment(int id, Fragment fragment) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(id, fragment);
        ft.commit();
    }

    public void toastShow(String text){
        Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
    }

    private ProgressHUD dialog;

    public void showWaitingDialog() {
        if (dialog == null) {
            dialog = ProgressHUD.show(this, "数据加载中", true, null);
        } else {
            dialog.show();
        }
    }

    public void cancelWaitingDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
            dialog = null;
        }
    }

    /**
     * 设置地址
     *
     * @param arrs
     */
    public void setUrlArrs(String... arrs) {
        if (arrs != null && arrs.length > 0) {
            urlArrs = arrs;
        }
    }

    /*
     *取消网络请求
     */
    public void cancelRequest(BasePresenter presenter, String... arr) {
        if (presenter != null && arr != null) {
            for (String url : arr) {
                presenter.cancelRequest(url);
            }
        }
    }

    /*
     *取消网络并且回收根视图
     */
    public void recycle(BasePresenter presenter, @Nullable String... arr) {
        cancelRequest(presenter, arr);
        setContentView(R.layout.activity_recycle_view);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppActivityManager.getInstance().removeActivity(this);//移除栈中的activity
        recycle(presenter, urlArrs);
    }
}
